#!/usr/bin/env python
# -*- coding:utf-8 -*- 

from flask_restful import Resource, reqparse
from flask import jsonify

from controller.templateController import TemplateController
from utils import commons
from utils.response_code import RET, error_map_EN
import json


class TemplateResource(Resource):

    # get
    @classmethod
    def get(cls, template_id=None):
        if template_id:
            kwargs = {
                'template_id': template_id
            }

            res = TemplateController.get(**kwargs)
            if res['code'] == RET.OK:
                return jsonify(code=res['code'], message=res['message'], data=res['data'])
            else:
                return jsonify(code=res['code'], message=res['message'], data=res['data'])

        parser = reqparse.RequestParser()
        parser.add_argument('auto_id', location='args', required=False, help='auto_id参数类型不正确或缺失')
        parser.add_argument('template_id', location='args', required=False, help='template_id参数类型不正确或缺失')
        parser.add_argument('user_id', location='args', required=False, help='user_id参数类型不正确或缺失')
        parser.add_argument('cover_pic', location='args', required=False, help='cover_pic参数类型不正确或缺失')
        parser.add_argument('location', location='args', required=False, help='location参数类型不正确或缺失')
        parser.add_argument('is_system', location='args', required=False, help='is_system参数类型不正确或缺失')
        parser.add_argument('create_time', location='args', required=False, help='create_time参数类型不正确或缺失')
        parser.add_argument('is_deleted', location='args', required=False, help='is_deleted参数类型不正确或缺失')
        
        parser.add_argument('Page', location='args', required=False, help='Page参数类型不正确或缺失')
        parser.add_argument('Size', location='args', required=False, help='Size参数类型不正确或缺失')

        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)

        res = TemplateController.get(**kwargs)
        if res['code'] == RET.OK:
            return jsonify(code=res['code'], message=res['message'], data=res['data'], totalPage=res['totalPage'], totalCount=res['totalCount'])
        else:
            return jsonify(code=res['code'], message=res['message'], data=res['data']) 

    # delete
    @classmethod
    def delete(cls):
        parser = reqparse.RequestParser()
        parser.add_argument('template_id', location='form', required=True, help='template_id 参数类型不正确或缺失')
        args = parser.parse_args()
        args = commons.put_remove_none(**args)

        res = TemplateController.delete(**args)

        return jsonify(code=res['code'], message=res['message'], data=res['data'])

    # put
    @classmethod
    def put(cls, template_id):
        if not template_id:
            return jsonify(code=RET.NODATA, message='primary key missed', error='primary key missed')

        parser = reqparse.RequestParser()
        parser.add_argument('user_id', location='form', required=False, help='user_id参数类型不正确或缺失')
        parser.add_argument('cover_pic', location='form', required=False, help='cover_pic参数类型不正确或缺失')
        parser.add_argument('location', location='form', required=False, help='location参数类型不正确或缺失')
        parser.add_argument('is_system', location='form', required=False, help='is_system参数类型不正确或缺失')
        parser.add_argument('create_time', location='form', required=False, help='create_time参数类型不正确或缺失')
        
        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)
        kwargs['template_id'] = template_id

        res = TemplateController.update(**kwargs)

        return jsonify(code=res['code'], message=res['message'], data=res['data'])

    # add
    @classmethod
    def post(cls):
        '''
        TemplateList: Pass in values in JSON format to batch add
        eg.[{k1:v1,k2:v2,...},...]
        '''
        parser = reqparse.RequestParser()
        parser.add_argument('TemplateList', type=str, location='form', required=False, help='TemplateList参数类型不正确或缺失')

        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)

        if kwargs.get('TemplateList'):
            kwargs['TemplateList'] = json.loads(kwargs['TemplateList'])
            for data in kwargs['TemplateList']:
                for key in []:
                    data.pop(key, None)
            res = TemplateController.add_list(**kwargs)

        else:
            parser.add_argument('user_id', location='form', required=False, help='user_id参数类型不正确或缺失')
            parser.add_argument('cover_pic', location='form', required=False, help='cover_pic参数类型不正确或缺失')
            parser.add_argument('location', location='form', required=False, help='location参数类型不正确或缺失')
            parser.add_argument('is_system', location='form', required=False, help='is_system参数类型不正确或缺失')
            parser.add_argument('create_time', location='form', required=False, help='create_time参数类型不正确或缺失')
            
            kwargs = parser.parse_args()
            kwargs = commons.put_remove_none(**kwargs)

            res = TemplateController.add(**kwargs)

        return jsonify(code=res['code'], message=res['message'], data=res['data'])
